package com.jic.interaction.JDVOP.controller;

import com.alibaba.fastjson.JSON;
import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.JDVOP.model.enyity.order.*;
import com.jic.interaction.JDVOP.model.vo.request.order.*;
import com.jic.interaction.JDVOP.model.vo.response.order.CheckCalendarResponse;
import com.jic.interaction.JDVOP.model.vo.response.order.CheckNewOrderResponse;
import com.jic.interaction.JDVOP.model.vo.response.order.ConfirmInfoResponse;
import com.jic.interaction.JDVOP.model.vo.response.order.OrderSubmitResponse;
import com.jic.interaction.JDVOP.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;

/**
 * @ClassName OrderController
 * TODO
 * @Description JD 订单接口
 * @Author chenhongfeng
 * @Date 2020/11/15 16:00
 * @Version 1.0
 * <p>
 * <p>
 * <p>
 * 7、订单API接口
 * 注意:下单接口调用前，建议先调用实时价格查询接口，判断价格是否发生变化，如变化提示客户价格变化，并刷新页面；价格无变化才调用下单接口下单；
 * 外部客户对接vop下单的流程，如下：
 * 1、客户调用下单接口，我们接口会明确返回下单成功或失败，见success字段
 * 2、客户下单使用三方订单号应该与京东订单号一一对应，下单失败，不能修改三方订单号重新下单（因为大客户系统会使用三方订单号进行防重处理）；
 * 3、如果客户下单失败，下单接口"success"会返回false。resultMessage会返回失败原因。客户可根据失败原因调整下单参数后，使用同一三方订单号，重新下单；
 * 4、下单失败有一种特殊情况是“重复下单”，会返回resultCode为"0008"。如果同一三方订单号已经存在有效订单，则视为重复下单，此时下单结果 result会返回该三方订单号对应订单信息，需要客户系统进行金额和商品、收货人等确认，如一致，可视为下单成功。
 * 5、下单成功后，可使用订单查询接口查询订单详细信息。
 * 6、如果客户调用下单接口，出现超时或其他异常，可稍微等待后，使用订单反查接口（https://bizapi.jd.com/api/order/selectJdOrderIdByThirdOrder）确认是否下单成功。
 * 7、支持礼品卡实物卡下单，但是只能下普票订单，不能跟非实物礼品卡混合下单。
 **/
@Slf4j
@RestController
@RequestMapping("/order")
@Api(value = "JD 订单接口", tags = "JD 订单接口", description = "OrderController")
public class OrderController {
    @Autowired
    OrderService service;

    /*
     * @Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *ClassName OrderController
     * @Description      查询准备提交的订单的运费 //TODO
     * @Date 16:15 2020/11/15
     * @Param
     * @return
     * @Version:1.0
     * 请求URL :https://bizapi.jd.com/api/order/getFreight
     * 访问方式："Content-Type", "application/x-www-form-urlencoded"
     **/
    @ApiOperation(value = "查询运费", notes = "查询运费。")
    @PostMapping("/getFreight")
    public RestResult<String> getOrderFreight(@RequestBody OrderFreightRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    log.info("进入了OrderController{}", JSON.toJSONString(request));
        OrderFreight freight=new OrderFreight();
        BeanUtils.copyProperties(request,freight);
        String s = service.selectFreightService(freight);

        return RestResult.success(s,"9999","");
    }

    /*
     *TODO 
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description 查询预约日历
     *@Date 21:06 2020/11/20
     *@Param 
     *@return 
     *@Version:1.0
     **/
    @ApiOperation(value = "查询预约日历", notes = "查询预约日历。")
    @PostMapping("/promiseCalendar")
        public  RestResult<CheckCalendarResponse> getPromiseCalendar(@RequestBody CheckCalendarRequest request){
        CheckCalendar param=new CheckCalendar();
        BeanUtils.copyProperties(request,param);
        CheckCalendarResponse checkCalendarResponse = service.selectPromiseCalendar(param);
        return RestResult.success(checkCalendarResponse,"0000", "接口未完善，正在通宵赶进度中");
        }



    /*
     * @Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *ClassName OrderController
     * @Description  提交订单信息，生成京东订单。 //TODO
     * @Date 17:35 2020/11/15
     * @Param OrderSubmitRequest
     * @return ResultResponse
     * @Version:1.0
     * URL :https://bizapi.jd.com/api/order/submitOrder
     **/
    @ApiOperation(value = "提交订单", notes = "提交订单。")
    @PostMapping("/submitOrder")
    public RestResult<OrderSubmitResponse> SubmitOrder(@RequestBody  OrderSubmitRequest request) {

        OrderSubmitResponse orderSubmitResponse =  service.SubmitOrder(request);

        return RestResult.error(null,"9999", "接口未完善，正在通宵赶进度中");
    }

    /*
     * @Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *ClassName OrderController
     * @Description 订单反查接口，根据第三方订单号反查京东的订单号。 //TODO 需要更改
     * @Date 17:46 2020/11/15
     * @Param token :授权时获取的access token Redis中获取。  thirdOrder:第三方订单号（非京东订单号）。
     * @return  京东的订单号。success为true，此字段为京东订单号。success为false，根据第三方订单号查询不到订单时，此字段返回null。
     * @Version:1.0
     *  URL:https://bizapi.jd.com/api/order/selectJdOrderIdByThirdOrder
     *  请求参数格式：key-value
     * 访问方式：application/x-www-form-urlencoded
     **/
    @ApiOperation(value = "反查订单", notes = "反查订单。")
    @PostMapping("/selectJdOrderIdByThirdOrder")
    public RestResult<String> getJdOrderIdByThirdOrderr(@RequestBody ContrastOrderRequest request) {
        ContrastOrder param=new ContrastOrder();
        BeanUtils.copyProperties(request,param);
        String result = service.selectJdOrderIdByThirdOrderr(param);
        return RestResult.error(result,"0000", "接口未完善，正在通宵赶进度中");
    }

    /*
     * @Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *ClassName OrderController
     * @Description 确认预占库存订单接口。 //TODO
     * @Date 18:07 2020/11/15
     * @Param token:授权时获取的access token redis获取  jdOrderId:京东的订单单号(下单返回的父订单号)
     * @return Boolean
     * @Version:1.0
     * URL :https://bizapi.jd.com/api/order/confirmOrder
     * 请求参数格式：key-value   访问方式：application/x-www-form-urlencoded
     **/
    @ApiOperation(value = "确认预占库存订单", notes = "确认预占库存订单。")
    @PostMapping("/confirmOrder")
    public RestResult<Boolean> getConfirmOrder(@RequestBody CampOnOrderRequest request) {
        CampOnOrder param=new CampOnOrder();
        BeanUtils.copyProperties(request,param);
        Boolean aBoolean = service.selectConfirmOrder(param);
        return RestResult.error(aBoolean,"9999", "接口未完善，正在通宵赶进度中");
    }

    /*
     *TODO
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description 取消未确认订单接口。
     *@Date 18:19 2020/11/15
     *@Param token:授权时获取的access redis 获取  jdOrderId:京东的订单单号(父订单号)
     *@return Boolean
     *@Version:1.0
     **/
    @ApiOperation(value = "取消未确认订单", notes = "取消未确认订单。")
    @PostMapping("/cancel")
    public RestResult<Boolean> getCancel(@RequestBody CancelOrderRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        CancelOrder param=new CancelOrder();
        BeanUtils.copyProperties(request,param);
        return service.selectCancel(param);
    }

    /*
     * @Author zonggaolei
     *@Package com.jic.interaction.JDVOP.controller
     *ClassName OrderController
     * @Description 查询订单详情。
     * @Date 16:31 2020/11/28
     * @Version:1.0
     * URL :https://bizapi.jd.com/api/order/selectJdOrder
     * 请求参数格式：key-value   访问方式：application/x-www-form-urlencodeddsa
     **/
    @ApiOperation(value = "查询订单详情", notes = "查询订单详情。")
    @PostMapping("/selectJdOrder")
    public RestResult<String> getOrderDetails(@RequestBody OrderDetailsRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        CampOnOrder param=new CampOnOrder();
        BeanUtils.copyProperties(request,param);
        return service.selectOrderDetails(request);
    }

    /*
     *TODO
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description 查询配送信息。
     *@Date 10:52 2020/11/16
     *@Param 
     *@return 
     *@Version:1.0
     **/
 @ApiOperation(value = "查询配送信息", notes = "查询配送信息。")
    @PostMapping("/orderTrack")
    public RestResult<String> getOrderTrack(@RequestBody OrderDeliveryRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
     OrderDelivery param=new OrderDelivery();
     BeanUtils.copyProperties(request,param);
     return service.selectOrderTrack(param);
    }
    /*
     *TODO 
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description 仅适用于厂商直送订单。厂商直送订单可使用此接口确认收货并将订单置为完成状态。
     *@Date 11:09 2020/11/16
     *@Param 
     *@return 
     *@Version:1.0
     **/
    @ApiOperation(value = "确认收货", notes = "确认收货。")
    @PostMapping("/confirmReceived")
    public RestResult<String> getConfirmReceived(@RequestBody CancelOrderRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
     CancelOrder param=new CancelOrder();
     BeanUtils.copyProperties(request,param);
     return service.selectConfirmReceived(param);
    }
    /*
   *TODO
   *ClassName OrderController
   *@Author chenhongfeng
   *@Package com.jic.interaction.JDVOP.controller
   *@Description 更新采购单号   更新订单上的PO单号，可选择用于配送单、发票等票面展示。
   *@Date 11:12 2020/11/16
   *@Param
   *@return
   *@Version:1.0
     **/
    @ApiOperation(value = " 更新采购单号", notes = "更新采购单号。")
    @PostMapping("/saveOrUpdatePoNo")
    public RestResult<Boolean> getSaveOrUpdatePoNo(@RequestBody OrderUpdateNumberRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        OrderUpdateNumber param=new OrderUpdateNumber();
        BeanUtils.copyProperties(request,param);
        Boolean aBoolean = service.selectSaveOrUpdatePoNo(param);
        return RestResult.success(aBoolean,"0000", "接口未完善，正在通宵赶进度中");
    }
    /*
     *TODO
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description 查询新建订单列表 查询所有新建的订单列表。可用于核对订单。
     *@Date 11:15 2020/11/16
     *@Param
     *@return
     *@Version:1.0
     **/
  @ApiOperation(value = "查询新建订单列表", notes = "查询新建订单列表。")
    @PostMapping("/checkNewOrder")
    public RestResult<CheckNewOrderResponse> getCheckNewOrder(@RequestBody CheckNewOrderRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
      CheckNewOrder param=new CheckNewOrder();
      BeanUtils.copyProperties(request,param);
      CheckNewOrderResponse checkNewOrderResponse = service.listCheckNewOrder(param);
      return RestResult.success( checkNewOrderResponse,"0000", "接口未完善，正在通宵赶进度中");
    }
    /*
     *TODO
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description 查询妥投订单列表 查询所有妥投的订单列表。可用于核对订单。
     *@Date 11:25 2020/11/16
     *@Param request
     *@return OrderResultResponse
     *@Version:1.0
     **/
    @ApiOperation(value = "查询妥投订单列表", notes = "查询妥投订单列表。")
    @PostMapping("/checkDlokOrder")
    public RestResult<CheckNewOrderResponse> getCheckDlokOrder (@RequestBody CheckNewOrderRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        CheckNewOrder param=new CheckNewOrder();
        BeanUtils.copyProperties(request,param);
        CheckNewOrderResponse checkNewOrderResponse = service.listCheckDlokOrder(param);
        return RestResult.error( checkNewOrderResponse,"9999", "接口未完善，正在通宵赶进度中");
    }
    /*
     *TODO
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description  查询拒收订单列表 查询所有拒收的订单列表。可用于核对订单。
     *@Date 11:28 2020/11/16
     *@Param 
     *@return 
     *@Version:1.0
     **/
    @ApiOperation(value = "查询拒收订单列表", notes = "查询拒收订单列表。")
    @PostMapping("/checkRefuseOrder")
    public RestResult<CheckNewOrderResponse> getCheckRefuseOrder (@RequestBody CheckNewOrderRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {


        return RestResult.error(null,"9999", "接口未完善，正在通宵赶进度中");
    }
    /*
     *TODO 
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description 
     *@Date 21:36 2020/11/20
     *@Param 
     *@return 
     *@Version:1.0
     **/
    @ApiOperation(value = "查询完成订单列表", notes = " 查询完成订单列表。")
    @PostMapping("/checkCompleteOrder")
    public RestResult<CheckNewOrderResponse> getCheckCompleteOrder (@RequestBody CheckNewOrderRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        CheckNewOrder param=new CheckNewOrder();
        BeanUtils.copyProperties(request,param);
        CheckNewOrderResponse checkNewOrderResponse = service.listCheckCompleteOrder(param);

        return RestResult.error(checkNewOrderResponse,"9999", "接口未完善，正在通宵赶进度中");
    }
    /*
     *TODO
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description
     *@Date 21:43 2020/11/20
     *@Param
     *@return
     *@Version:1.0
     **/
    @ApiOperation(value = "查询配送预计送达时间", notes = "查询配送预计送达时间。")
    @PostMapping("/getPromiseTips")
    public RestResult<String> getPromiseTips (@RequestBody OrderPromiseTipsRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {


        return RestResult.error(null,"9999", "接口未完善，正在通宵赶进度中");
    }
    /*
     *TODO
     *ClassName OrderController
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.controller
     *@Description 批量确认收货接口
     *@Date 21:43 2020/11/20
     *@Param
     *@return
     *@Version:1.0
     **/
    @ApiOperation(value = "批量确认收货接口", notes = "批量确认收货接口。")
    @PostMapping("/batchConfirmReceived")
    public RestResult<ConfirmInfoResponse> getBatchConfirmReceived (@RequestBody BatchOrderRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {


        return RestResult.error(null,"9999", "接口未完善，正在通宵赶进度中");
    }

    @ApiOperation(value = "填写运单信息", notes = "填写运单信息。")
    @PostMapping("/updateSendInfo")
    public RestResult<String> updateSendInfo(@RequestBody OrderWaybillInformationRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        OrderWaybillInformation orderWaybillInformation = new OrderWaybillInformation();
        BeanUtils.copyProperties(request,orderWaybillInformation);
        return service.updateSendInfo(orderWaybillInformation);
    }





}
